package com.ddxcloud.movie.config.wechat;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "wx.pay")
@ConditionalOnClass(WxPayService.class)
public class WxPayConfiguration {

    private String appId;
    private String mchId;
    private String apiV3key;
    private String privateKeyPath;
    private String privateCertPath;
    private String jsapiAppId;
    private String jsapiSecret;

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(mchId);
        payConfig.setApiV3Key(apiV3key);
        payConfig.setPrivateKeyPath(privateKeyPath);
        payConfig.setPrivateCertPath(privateCertPath);

        // 沙箱环境（V3版本不支持沙箱环境）
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }
}
